define(function (require, exports, module) {

    // 通过 require 引入依赖
    // var $ = require('jquery');
    require('common');
    require('sui');
    var template = require('template');
    dateFormat(template);
    var rows = 20;
    var channelId = $('#channelId').val();

    function initData() {
        getUserData(1, rows, channelId);
        getGameDelData(1, rows, channelId);
    }

    $('#form-channel').validate({
        rules: {
            channelName: {
                required: true,
                minlength: 2,
                maxlength: 20
            }
        },
        messages: {
            channelName: "请填写"
        },
        success: function () {
            createChannel();
            return false;
        }
    });

    $(document).on('click', '.check-add', function () {
        if ($(this).is(":checked")) {
            $('input[name="channel-add-id"]').prop("checked", true);//全选
            $('#tab-add tbody tr').addClass('tr-click');
        } else {
            $('input[name="channel-add-id"]').prop("checked", false);//取消全选
            $('#tab-add tbody tr').removeClass('tr-click')
        }
    }).on('click', '.query-add', function () {
        getUserData(1, rows, channelId);
    }).on('click', '.add-game-all', function () {
        var inputs = $('input[name="channel-add-id"]');
        var ids = new Array();
        $.each(inputs, function (i, item) {
            if ($(item).is(':checked')) {
                // ids[i] = parseInt($(item).val());
                ids.push(parseInt($(item).val()));
            }
        });
        if (ids.length == 0) {
            toastInfo('请选择要添加的游戏');
            return;
        }
        $.post('/channel/addGame', {ids: JSON.stringify(ids), channelId: channelId}, function (data) {
            switch (data.code) {
                case 0:
                    toastSuccess('添加成功');
                    getGameAddData(1, rows, channelId);
                    getGameDelData(1, rows, channelId);
                    break;
                default :
                    toastError('添加失败');
            }
        });
    }).on('click', '.check-del', function () {
        if ($(this).is(":checked")) {
            $('input[name="channel-del-id"]').prop("checked", true);//全选
        } else {
            $('input[name="channel-del-id"]').prop("checked", false);//取消全选
        }
    }).on('click', '.query-del', function () {
        getGameDelData(1, rows, channelId);
    }).on('click', '.del-game-all', function () {
        var inputs = $('input[name="channel-del-id"]');
        var ids = new Array();
        $.each(inputs, function (i, item) {
            if ($(item).is(':checked')) {
                // ids[i] = parseInt($(item).val());
                ids.push(parseInt($(item).val()));
            }
        });
        if (ids.length == 0) {
            toastInfo('请选择要删除的游戏');
            return;
        }
        $.post('/channel/delGame', {ids: JSON.stringify(ids), channelId: channelId}, function (data) {
            switch (data.code) {
                case 0:
                    toastSuccess('删除成功');
                    getGameDelData(1, rows, channelId);
                    break;
                default :
                    toastError('删除失败');
            }
        });
    });

    function createChannel() {
        var channelName = $.trim($('#channelName').val());
        var plateformType = $('#plateformType').val();
        var channelRemark = $.trim($('#channelRemark').val());
        var data = {channelName: channelName, plateformType: plateformType, channelRemark: channelRemark};
        $('.create-channel').prop('disabled', 'disabled');
        $.post('/channel/addChannel', data, function (o) {
            switch (o.code) {
                case 0:
                    var id = o.data;
                    channelId = id;
                    toastSuccess('新增渠道成功');
                    $('.add-game-all').removeAttr('disabled');
                    break;
                default:
                    toastError(o.msg);
                    $('.create-channel').removeAttr('disabled');
            }
        })

    }

    function getUserData(page, rows, channelId) {
        var accountName = $.trim($('#accountName').val());
        var paccountName = $.trim($('#paccountName').val());
        var beginTime = $.trim($('#beginTime').val());
        var endTime = $.trim($('#endTime').val());
        var data = {
            channelId: channelId,
            accountName: accountName,
            paccountName: paccountName,
            beginTime: beginTime,
            endTime: endTime,
            page: page,
            rows: rows
        };
        $.post('/channel/listUser', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-add', {list: list});
                    $('#tab-add tbody').empty();
                    $('#tab-add tbody').html(html);
                    $('.add-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询游戏数据失败');
            }
        });
    }

    function getGameDelData(page, rows, channelId) {
        var plateformType = $.trim($('#plateformType2').val());
        var gameType = $.trim($('#gameType2').val());
        var gameName = $.trim($('#gameName2').val());
        var data = {
            plateformType: plateformType,
            gameType: gameType,
            gameName: gameName,
            page: page,
            rows: rows,
            channelHasAddFlag: 1,
            channelId: channelId
        };
        $.post('/channel/listGame', data, function (data) {
            switch (data.code) {
                case 0:
                    var list = data.data.list;
                    var count = data.data.count;
                    var html = template('tp-del', {list: list});
                    $('#tab-del tbody').empty();
                    $('#tab-del tbody').html(html);
                    $('.del-pagination').pagination('updateItemsCount', count);
                    break;
                default:
                    toastError('查询数据失败');
            }
        });
    }

    $('.add-pagination').pagination({
        itemsCount: 0,
        pageSize: rows,
        showCtrl: true,
        displayPage: 6,
        onSelect: function (num) {
            getUserData(num, rows, channelId)
        }
    });

    $('.del-pagination').pagination({
        itemsCount: 0,
        pageSize: rows,
        showCtrl: true,
        displayPage: 6,
        onSelect: function (num) {
            getGameDelData(num, rows, channelId)
        }
    });

    function dateFormat(template) {
        template.helper('dateFormat', function (date, format) {
            date = new Date(date);
            var map = {
                "M": date.getMonth() + 1, //月份
                "d": date.getDate(), //日
                "h": date.getHours(), //小时
                "m": date.getMinutes(), //分
                "s": date.getSeconds(), //秒
                "q": Math.floor((date.getMonth() + 3) / 3), //季度
                "S": date.getMilliseconds() //毫秒
            };
            format = format.replace(/([yMdhmsqS])+/g, function (all, t) {
                var v = map[t];
                if (v !== undefined) {
                    if (all.length > 1) {
                        v = '0' + v;
                        v = v.substr(v.length - 2);
                    }
                    return v;
                }
                else if (t === 'y') {
                    return (date.getFullYear() + '').substr(4 - all.length);
                }
                return all;
            });
            return format;
        });
    }

    initData();
});